//
//  UIButton+XYLayoutType.m
//  PeiJue
//
//  Created by lxy on 2017/10/12.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "UIButton+XYLayoutType.h"

@implementation UIButton (XYLayoutType)

- (void)xy_layoutButtonTitleImgWithStyle:(UIButtonLayoutType)layoutType titleImgSpace:(CGFloat)space {
    //获取label的宽和高
    CGFloat labelW = 0;
    CGFloat labelH = 0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        labelW = self.titleLabel.intrinsicContentSize.width;
        labelH = self.titleLabel.intrinsicContentSize.height;
    }else{
        labelW = self.titleLabel.frame.size.width;
        labelH = self.titleLabel.frame.size.height;
    }
    
    //获取imageView的宽和高
    CGFloat imageW = self.imageView.frame.size.width;
    CGFloat imageH = self.imageView.frame.size.height;
    
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets titleEdgeInsets = UIEdgeInsetsZero;
    
    switch (layoutType) {
        case UIButtonLayoutTypeLeft://文字在左 图片在右
            imageEdgeInsets = UIEdgeInsetsMake(0, labelW + space / 2, 0, -labelW - space / 2);
            titleEdgeInsets = UIEdgeInsetsMake(0, -imageW - space / 2, 0, imageW + space / 2);
            break;
        case UIButtonLayoutTypeRight://文字在右 图片在左
            imageEdgeInsets = UIEdgeInsetsMake(0, -space / 2, 0, space / 2);
            titleEdgeInsets = UIEdgeInsetsMake(0, space / 2, 0, -space / 2);
            break;
        case UIButtonLayoutTypeTop:// 文字在上 图片在下
            imageEdgeInsets = UIEdgeInsetsMake(labelH / 2 + space / 4, labelW / 2, -labelH / 2 - space / 4, -labelW / 2);
            titleEdgeInsets = UIEdgeInsetsMake(-imageH / 2 - space / 4, - imageW / 2, imageH / 2 + space / 4, imageW / 2);
            break;
        case UIButtonLayoutTypeBottom://文字在下 图片在上
            imageEdgeInsets = UIEdgeInsetsMake(-labelH / 2 - space / 4, labelW / 2, labelH / 2 + space / 4, - labelW / 2);
            titleEdgeInsets = UIEdgeInsetsMake(imageH / 2 + space / 4, -imageW / 2, - imageH / 2 - space / 4, imageW / 2);
            
            break;
            
        default:
            break;
    }
    
    self.titleEdgeInsets = titleEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}


@end
